#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Project ：turbo 
@File    ：viewsets.py
@Author  ：赵加响
@Date    ：2023/4/3 14:45 
@Desc    ：
"""
from rest_framework.filters import OrderingFilter, SearchFilter
from rest_framework.viewsets import GenericViewSet

from bases import mixins
from bases.backends import CustomDjangoFilterBackend
from bases.pagination import APIPageNumberPaginationWithDefaultPageSize


class APIReadOnlyViewSet(mixins.APIRetrieveModelMixin,
                         mixins.APIListModelMixin,
                         GenericViewSet):
    """
    集成只可以读取数据的ViewSet
    """
    filterset_fields = None
    filter_fields = None  # django-filter==22.1版本后修改为filterset_fields，自定义加上该属性
    filterset_class = None
    filter_class = None  # django-filter==22.1版本后修改为filterset_class，自定义加上该属性
    pagination_class = APIPageNumberPaginationWithDefaultPageSize
    filter_backends = [CustomDjangoFilterBackend, OrderingFilter, SearchFilter]


class APICreateOnlyViewSet(APIReadOnlyViewSet,
                           mixins.APICreateModelMixin):
    """
    集成可以新建读取数据的ViewSet
    """
    pass


class APIUpdateOnlyViewSet(APIReadOnlyViewSet,
                           mixins.APIUpdateModelMixin):
    """
    集成可以更新读取数据的ViewSet
    """
    pass


class APIDeleteOnlyViewSet(APIReadOnlyViewSet,
                           mixins.APIDestroyModelMixin):
    """
    集成可以删除读取数据的ViewSet
    """
    pass


class APIModelViewSet(APIReadOnlyViewSet,
                      mixins.APICreateModelMixin,
                      mixins.APIUpdateModelMixin,
                      mixins.APIDestroyModelMixin):
    """
    集成可以对数据进行增删改查的ViewSet
    """
    pass


class APIRawQueryViewSet(mixins.APIRawQueryMixin,
                         GenericViewSet):
    """
    集成使用原生sql只读数据的ViewSet
    raw_query为必输项
    filter_rule为非必输项
    database_name默认为default数据库，用于指定从哪个数据库中查询
    """
    pagination_class = APIPageNumberPaginationWithDefaultPageSize
    raw_query = None
    filter_rule = None
    database_name = 'default'
